import { ref } from 'vue'
import { defineStore } from 'pinia'
import type { CategoryInfo, CategoryList } from '@/services/api/category'

// 类型声明

type CategoryListOrEmpty = CategoryList | []

export const useCategoryStore = defineStore('category', () => {
  const categoryMap = ref<any>()
  const categoryList = ref<CategoryListOrEmpty>([])
  const currentPCategory = ref<any>({})

  // 保存分类列表
  const saveCategoryList = (data: CategoryListOrEmpty) => {
    categoryList.value = data
  }

  //保存分类map数据
  const saveCategoryMap = (data: CategoryInfo) => {
    categoryMap.value = data
  }

  //保存当前分类的父级ID
  const saveCurrentPCategory = (data: any) => {
    if (data != null) {
      console.log(categoryMap.value, '@@@')

      console.log(categoryMap.value?.[data], '@@@')

      currentPCategory.value = categoryMap.value?.[data] || {}
    } else {
      currentPCategory.value = {}
    }
  }

  return {
    categoryMap,
    categoryList,
    currentPCategory,
    saveCategoryList,
    saveCategoryMap,
    saveCurrentPCategory
  }
})
